package cn.shop.merchant.service;

import cn.shop.transcation.inbox.mapper.InboxMapper;
import cn.shop.transcation.inbox.model.Inbox;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 站内信service
 *
 */
@Service
public class InboxService extends BaseServiceImpl<InboxMapper, Inbox> {

    public long getUnreadCount(long uid) {
        return count(Wrappers.<Inbox>lambdaQuery()
                .eq(Inbox::getUserId, uid)
                .eq(Inbox::isRead, false));
    }

    public List<Map<String, Object>> inboxList(long uid) {
        List<Inbox> list = list(Wrappers.<Inbox>lambdaQuery()
                .eq(Inbox::getUserId, uid)
                .orderByDesc(Inbox::getId));
        return list.stream().map(this::formant).collect(Collectors.toList());
    }

    public boolean read(List<Long> ids) {
        Inbox update = new Inbox();
        update.setRead(true);
        return update(update, Wrappers.<Inbox>lambdaQuery().in(Inbox::getId, ids));
    }

    private Map<String, Object> formant(Inbox inbox) {
        return Putif.start()
                .anyway("title", inbox.getTitle())
                .anyway("content", inbox.getContent())
                .anyway("time", inbox.getCreateTime())
                .anyway("id", inbox.getId())
                .get();
    }

}
